package com.mijie.homi.ns.service;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mijie.homi.common.constant.MCKey;
import com.mijie.homi.common.domain.user.DeviceInfo;
import com.mijie.homi.common.domain.user.Setting;
import com.mijie.homi.common.domain.user.ThirdPartAccount;
import com.mijie.homi.common.domain.user.User;
import com.mijie.homi.common.domain.user.UserExtInfo;
import com.mijie.homi.ns.dao.UserDao;
import com.mijie.util.DateUtil;

@Service
public class UserService {

	@Autowired
	private UserDao dao;
	@Autowired
	private MemcacheService mcs;
	private Logger logger = Logger.getLogger(UserService.class);
	
	public synchronized void incrOnlineUserAmount(){
		long amount = mcs.incr(MCKey.ONLINE_USER_AMOUNT, 1);
		Long max = (Long) mcs.get(MCKey.MAX_ONLINE_USER_AMOUNT, "");
		if(max==null || amount>max){
			mcs.set(MCKey.MAX_ONLINE_USER_AMOUNT, "", 0, new Long(amount));
			mcs.set(MCKey.MAX_ONLINE_STAT_AT, "", DateUtil.date2str(new Date(), true));
		}
	}
	
	public synchronized void decrOnlineUserAmount(){
		long amount = mcs.decr(MCKey.ONLINE_USER_AMOUNT, 1);
	}
	
	public void resetOnlineUserAmount(){
		mcs.delete(MCKey.ONLINE_USER_AMOUNT, "");
	}
	
	public int updateLastLoginAt(int id,String os,String deviceId,String clientIP){
		int rows =  dao.updateLastLoginAt(id,os,clientIP);
		if(rows>0){
			mcs.delete(MCKey.USER_EXTINFO_BY_ID, String.valueOf(id));
			dao.resetDeviceUnreadNotifyCount(deviceId);
		}
		return rows;
	}
	
	public DeviceInfo getDeviceInfo(String deviceId){
		return dao.getDeviceInfo(deviceId);
	}
	
	public User getUserById(int id){
		User user = (User) mcs.get(MCKey.USER_BY_ID, String.valueOf(id));
		if(user==null){
			user = dao.getUserById(id);
			if(user!=null) mcs.set(MCKey.USER_BY_ID, String.valueOf(id), MCKey.USER_CACHE_TIMEOUT, user);
		}
		return  user;
	}
	
	public List<User> listUserByNicknameAndPassword(String nickName,String passwd){
		return dao.listUserByNicknameAndPassword(nickName, passwd);
	}
	
	public User getUserByEmail(String email){
		return dao.getUserByEmail(email);
	}
	
	public UserExtInfo getUserExtInfoByUserId(int userId){
		UserExtInfo info = (UserExtInfo) mcs.get(MCKey.USER_EXTINFO_BY_ID, String.valueOf(userId));
		if(info==null){
			info =  dao.getUserExtInfoByUserId(userId);
			if(info!=null) mcs.set(MCKey.USER_EXTINFO_BY_ID, String.valueOf(userId), MCKey.USER_CACHE_TIMEOUT, info);
		}
		return info;
	}
	
	public int incrOnlineLong(int id,int onlineLong,boolean online){
		mcs.delete(MCKey.USER_EXTINFO_BY_ID, String.valueOf(id));
		return dao.incrOnlineLong(id, onlineLong,online?1:0);
	}
	
	public int deleteUserInfoSubcribe(int subscriber){
		return dao.deleteUserInfoSubcribe(subscriber);
	}
	
	public List<ThirdPartAccount> listThirdPartAccountByUser(int userId){
		return dao.listThirdPartAccountByUser(userId);
	}
	
	public Setting getSettingByUser(int userId){
		Setting setting = (Setting) mcs.get(MCKey.USER_SETTING, String.valueOf(userId));
		if(setting==null){
			setting = dao.getSettingByUser(userId);
			if(setting!=null){
				mcs.set(MCKey.USER_SETTING, String.valueOf(userId), MCKey.USER_CACHE_TIMEOUT, setting);
			}
		}
		return setting;
	}
}
